var app = getApp()
var IP = app.globalData.IP
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:'',
    selected: '',
    serviceid: '',
    imageurl: '',
    servicename: '',
    partslist: [],
    totalprice: '',
    status: ['', '', 'background-color: #65bec7', 'background-color: #65bec7'],
    selectedpartslist:[],
    selectedguzhang:{},

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    const eventChannel = this.getOpenerEventChannel()
    var that = this
    eventChannel.on('data', function (data) {
      that.setData({
        selected: data.selected,
        serviceid: data.serviceid,
        imageurl: data.imageurl,
        servicename: data.servicename,
        selectedguzhang:data.selectedguzhang,
        id:data.id
      })
      console.log(data)
    })


    this.getparts()


  },

  click(e) {
    var that = this
    var this_checked = e.currentTarget.dataset.clickid
    var parts = this.data.partslist //获取数组

    for (var i = 0; i < parts.length; i++) {
      if (parts[i].id == this_checked) {
        //当前点击的位置为true即选中
        if (parts[i].id == this_checked & parts[i].checked == undefined) {
          parts[i].checked = true;
        } else if (parts[i].id == this_checked & parts[i].checked == false) {
          parts[i].checked = true;
        } else if (parts[i].id == this_checked & parts[i].checked == true) {
          parts[i].checked = false
        }
      }


      //totalprice

    }
    that.setData({
      partslist: parts
    })

    this.calculate(this.data.partslist)



  },

  calculate(parts) {
    var totalprice = 0;
    for (var i = 0; i < parts.length; i++) {
      if (parts[i].checked == true) {

        totalprice += Number(parts[i].partprice)
      }
    }
    this.setData({
      totalprice: totalprice
    })
  },


  getparts() {
    wx.request({
      url: `http://${IP}:8080/employee/getparts`,
      data: {
        serviceid: this.data.serviceid,
        category: '配件'
      },
      success: res => {
        this.setData({
          partslist: res.data
        })

        console.log(res.data)
      }
    })
  },

  navito() {

    this.setData({selectedpartslist:[]})
    var parts=this.data.partslist
    for(var i = 0; i < parts.length; i++){
      if (parts[i].checked == true) {
        this.data.selectedpartslist.push(parts[i])
      }


    }
    wx.navigateTo({
      url: '/pages/taximeter/detail5/detail5',
      success: res => {
        res.eventChannel.emit('data', {
          selected: this.data.selected+">",
          serviceid: this.data.serviceid,
          imageurl: this.data.imageurl,
          servicename: this.data.servicename,
          totalprice:this.data.totalprice,
          selectedguzhang:this.data.selectedguzhang,
          parts:this.data.selectedpartslist,
          id:this.data.id
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})